/*===============================================================================
=============================Data Grid===========================================
===============================================================================*/
/**
 * 获得Datagrid中选中的隐藏项的value 
 * @param grid grid对象
 * @param varname 需要获得的隐藏项的名称
 * @returns {String}
 */
function getSelectedHiddenVal(grid, varname){
	var selectedrowids = grid.getSelectedRowId();
	if (!selectedrowids)
		return;
	var rowids = selectedrowids.split(",");
	var str = '';
	for ( var i = 0; i < rowids.length; i++) {
		str += grid.cells(rowids[i], 0).getValue();
	}
	var obj = _parseDom(str);
	if (!obj)
		return;
	var inputs = obj.getElementsByTagName("input");
	var cks = '';
	for ( var i = 0; i < inputs.length; i++)
		if (inputs[i].name == varname)
			cks += inputs[i].value + ",";

	// alert(cks);
	if (cks.length > 0)
		if (cks.substring(cks.length, cks.length - 1) == ',')
			cks = cks.substring(0, cks.length - 1);
	return cks;

}

function _parseDom(arg) {
	var objE = document.createElement("div");
	objE.innerHTML = arg;
	return objE;
}

function _checkall(grid, event){
	
	if (event.target.checked)
		grid.selectAll();
	else
		grid.clearSelection();
}
/*===============================================================================
=============================共用方法=============================================
===============================================================================*/
/*function wShowMD(url, width, height){
	if (!width)
		width = 600;
	if (!height)
		height = 400;
	var WWidth = width;
	var WHight = height;
    var WLeft=(window.screen.width-WWidth)/2;
	var WTop=(window.screen.height-WHight)/2;
	var WUrl = url;
	window.showModalDialog(WUrl,window,"dialogLeft="+WLeft + "px;dialogTop=" +WTop +"px;dialogWidth="+width+"px;dialogHeight="+height+"px;resizable=yes;status=Yes;");
}*/
function wShowMD(url, _width, _height){
	var wd = document.getElementById("iframepopup");
	if (!wd){
		wd = window.parent.document.getElementById("iframepopup");
		if (!wd)
			return;
	}
	
	if (!_width)
		_width = 400;
	if (!_height)
		_height = 400;
	
	mask(1);
	wd.src = url;
	wd.style.width = _width + 'px';
	wd.style.height = _height + 'px';
	
	
	var width, height;
	if (navigator.userAgent.indexOf("MSIE") > 0 || navigator.userAgent.indexOf("Firefox") > 0){
		width = document.documentElement.scrollWidth;
		height = document.documentElement.scrollHeight;
	}else if (navigator.userAgent.indexOf("Safari") > 0){
		width = document.body.scrollWidth;
		height = document.body.scrollHeight;
	}else{
		width = 1024;
		height = 650;
	}
	
	wd.style.top = ((height - _height) / 2) + "px";
	wd.style.left = ((width - _width) / 2) + "px";
	wd.style.display = '';
}

function wCloseMD(){
	var wd = document.getElementById("iframepopup");
	if (!wd){
		wd = window.parent.document.getElementById("iframepopup");
		if (!wd)
			return;
	}
	wd.src = "about:blank";
	wd.style.display = "none";
	mask();
}

//隐藏显示表格
function showHiddenTbl(){
	var tablename = "searchpanel";
	if (!document.all(tablename))
		return;
    if (document.all(tablename).style.display == 'none') 
        document.all(tablename).style.display = '';
    else 
        document.all(tablename).style.display = 'none';
}

function sortSearch() {
	var orderby, sortby;
	
	if (!document.all("page.orderBy") || !document.all("page.sortby")){
		alert("err 1");
		return;
	}
	
	if (!event.srcElement.id || event.srcElement.id == ''){
		alert("err 2");
		return;
	}
	
	orderby = document.all("page.orderby").value;
	sortby = document.all("page.sortby").value;
	
	if (sortby == '')
		sortby = "ASC";
	else{
		if (orderby != event.srcElement.id)
			sortby = "ASC";
		else{
			if (sortby == "ASC")
				sortby = "DESC";
			else
				sortby = "ASC";
		}
	}
	orderby = event.srcElement.id;
	
	document.all("page.orderby").value = orderby;
	document.all("page.sortby").value = sortby;
	
	//alert(orderby + "|" + sortby);
	document.all("frmsearch").submit();
}

function initSharingpool(orderby, sortby){
	if (orderby == "")
		return;
	if (!document.all(orderby))
		return;
	if (sortby == "ASC")
		document.all(orderby).innerHTML += "    <img src='/resource/img/arrow_up.gif'>";
	else
		document.all(orderby).innerHTML += "    <img src='/resource/img/arrow_down.gif'>";
}

function mask(flag){
	var _mask = document.getElementById("_mask");
	if (!_mask){
		_mask = window.parent.document.getElementById("_mask");
		if (!mask)
			return;
	}
		
	if (1 == flag){
 		_mask.style.width = "100%";
		_mask.style.height = "100%";
		_mask.style.top = 0;
		_mask.style.left = 0;
		_mask.style.display = '';
	}else{
		_mask.style.display = "none";
	}
}

/* ============================================================================================= */
/*==========================================EasyUI===============================================*/
/* ============================================================================================= */
// open a window
// params:
// winid: id that window div
// url:open page url
// title:open page title
function open(winid, url, title, _width, _height, _modal) {
	if (!_modal) {
		_modal = true;
	}
	winObj=$('#'+winid);
	//remove all element in win div contains text
	winObj.empty();
	//dynamic create iframe in win div
	$("<iframe src='"+url+"' style='width:100%;height:100%;overflow:hidden' frameborder='0' scrolling='no' id="+winid+"'Frame'></iframe>").prependTo(winObj);
	if (_width && _height) {

		winObj.window({
			modal : _modal,
			title : title,
			width : _width,
			height : _height,
			left:10,
			top:10,
			cache:false,
			maximizable:false
			//href : url
		});
	} else {
		winObj.window({
			modal : _modal,
			title : title,
			cache:false
			//href : url
		});
	}
	winObj.window('open');
}

//open a window
//params:
/*
options={
winid:'winid',
url:'newwin.jsp',
title:'New Window',
width:400,
height:200,
left:10,
top:10,
modal:true,
overflow:'hidden',
scrolling:'yes'
}
*/
function openAdvance(options){
	if(!options.winid){
		options.winid='win';
	}
	if (!options.modal) {
		options.modal = true;
	}
	if (!options.overflow) {
		options.overflow = 'hidden';
	}
	if (!options.scrolling) {
		options.scrolling = 'no';
	}
	if (!options.top) {
		options.top = 10;
	}
	if (!options.left) {
		options.left = 10;
	}
	winObj=$('#'+options.winid);
	//remove all element in win div contains text
	winObj.empty();
	//dynamic create iframe in win div
	$("<iframe src='"+options.url+"' style='width:100%;height:100%;overflow:"+options.overflow+ ";' frameborder='0' scrolling='"+options.scrolling+"' id="+options.winid+"'Frame'></iframe>").prependTo(winObj);
	if (options.width && options.height) {

		winObj.window({
			modal :options.modal,
			title :options.title,
			width :options.width,
			height:options.height,
			left:options.left,
			top:options.top,
			cache:false,
			maximizable:true
		});
	} else {
		winObj.window({
			modal : options.modal,
			title : options.title,
			cache:false
		});
	}
	winObj.window('open');
}


// open window, which id is 'win' div
function openWindow(url, title, _width, _height, _modal) {
	open('win', url, title, _width, _height, _modal);
}

function closeWindow(){
	$('#win').window('close');
}

// parse string to JSON object
/**
 * strData:JSON String
 * return JSON Object
 */
function parseToJson( strData ){
    return (new Function( "return " + strData ))();
}


/******************************************************************************/
//--------------------------Tool Bar Function Utils---------------------------//
function create(options) {
	openAdvance(options);
}

function view(options) {
	id=$("#lister").jqGrid('getGridParam','selarrrow');
	id=id.toString();
	if (id===""||id.split(",").length==0) {
		$.messager.alert('警告','请选择一条记录！','warning');
		return;
	}
	if (id.split(",").length > 1) {
		$.messager.alert('警告','只能选一条记录！','warning');
		return;
	}
	var url=options.url+"";
	var index=url.indexOf("?");
	if(index==-1){
		url=options.url+'?bean.id='+id;
	}else{
		url=url.substring(0,index);
		url=url+'?bean.id='+id;
	}
	options.url=url;
	//openWindow(url+'?bean.id='+id,title,_width, _height);
	openAdvance(options);
}

function modify(options) {
	id=$("#lister").jqGrid('getGridParam','selarrrow');
	id=id.toString();
	if (id===""||id.split(",").length==0) {
		$.messager.alert('警告','请选择一条记录！','warning');
		return;
	}
	if (id.split(",").length > 1) {
		$.messager.alert('警告','只能选一条记录！','warning');
		return;
	}
	var url=options.url+"";
	var index=url.indexOf("?");
	if(index==-1){
		url=options.url+'?bean.id='+id;
	}else{
		url=url.substring(0,index);
		url=url+'?bean.id='+id;
	}
	options.url=url;
	//openWindow(url+'?bean.id='+ id,title,_width, _height);
	openAdvance(options);
}

function remove(url){
	var gr = $("#lister").jqGrid('getGridParam','selarrrow');
	var options={
		url:url+'?beanIds='+gr.toString(),
		reloadAfterSubmit:false
	};
	if(gr!=null && gr.toString()!="")$('#lister').jqGrid('delGridRow', gr,options );
	else $.messager.alert('警告','请至少选择一条记录！','warning');
}

/**
 * 
 * @param options url and title
 * @param type C,V,M,R
 */
function toDo(options,type){
	switch(type){
	case 'C':
		create(options.create);
		break;
	case 'V':
		view(options.view);
		break;
	case 'M':
		modify(options.modify);
		break;
	case 'R':
		remove(options.remove.url);
		break;
	}
}



function updateGrid(jsonObj){
	$("#lister").trigger("reloadGrid");
	closeWindow();
}

/*****************************************************************************/


/**
 * get input file absolute path for IE and Fixfox
 */
function getPath(obj)
{
   if(obj)
    {

     if (window.navigator.userAgent.indexOf("MSIE")>=1)
      {
        obj.select();

       return document.selection.createRange().text;
      }

     else if(window.navigator.userAgent.indexOf("Firefox")>=1)
      {
       if(obj.files)
        {

         return obj.files.item(0).getAsDataURL();
        }
       return obj.value;
      }
     return obj.value;
    }
 }